package com.sinodata.bsm.cicp.collector.ha;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 双机--> 节点 的 采集程序， 采集节点状态
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 9:57:47    liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class HANodeCollector extends Collector {

    private static final Logger logger = Logger.getLogger(HANodeCollector.class);

    /**
     * 采集方法
     * 
     * @param ip 执行命令的主机ip地址
     * @return 资源列表
     * @throws IOException
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> resProValList = null;
        BufferedReader reader = null;
        try {
            reader = CollectorUtil.getReader("clstat -o", ip);
            // 解析命令的响应结果
            resProValList = this.parseReader(reader);
        } catch (IOException e) {
            logger.error("Failed to collector HANode", e);
            throw new CollectorException("Failed to collector HANode ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO Resource", e);
                    throw new CollectorException("Failed to close IO Resource ", e);
                }
            }
        }
        return resProValList;
    }

    /**
     * 解析 响应结果
     * 
     * @param reader 响应结果
     * @return 资源列表
     * @throws IOException
     */
    private List<ResPropertyValue> parseReader(BufferedReader reader) throws IOException {
        List<ResPropertyValue> resProValList = null;
        String typeStr = this.getParamMap().get(getParamMeta()[0]);
        // 匹配（目标数据，没有检查条件）： Node : xxx State ：xxx
        Pattern typepatn = Pattern.compile("\\s*Node\\s*:\\s*(\\S*)\\s*\\S*\\s*:\\s*(\\S*)\\s*");
        String[] propMetas = getPropMeta();
        String tmp = null;
        Matcher typeMach = null;
        resProValList = new ArrayList<ResPropertyValue>();
        while ((tmp = reader.readLine()) != null) {
            typeMach = typepatn.matcher(tmp);
            if (typeMach.matches()) {
                // 只找指定名称 的 节点的状态
                if (typeStr.indexOf(typeMach.group(1)) > -1) {
                    ResPropertyValue propertyValueX = new ResPropertyValue();
                    ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                    propValues[0] = ResPropertyValue.createPropValue(typeMach.group(2), propMetas[0]);
                    propertyValueX.setValues(propValues);
                    resProValList.add(propertyValueX);
                }
            }
        }
        return resProValList;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "Node Name" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "Node State" };
    }

}
